// File:    TnCalcThua3020.cs
// Author:  HT
// Created: Monday, October 17, 2011 3:25:21 PM
// Purpose: Definition of Class TnCalcThua3020

using System;
using System.ComponentModel;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using System.Collections.Generic;

namespace Tayninh2011
{
    public class TnCalcThua3020 : TnToolsCalculator
    {
        //constructor
        
        public TnCalcThua3020()
        {
            this.name = "calculator 3020";

            this.startCaculator = new startCaculatorDelegate(initForDelegate);
        }
        public TnCalcThua3020(IWorkspaceEdit workspace, string environment)
        {
            this.name = "calculator 3020";
            this.startCaculator = new startCaculatorDelegate(initForDelegate);
            this.workspaceEdit = workspace;
            this.environment = environment;
        }
        public override void AddTask(EnumTask task)
        {
            TnThua thua;
            TnDuong duong;
            object[] obj = (this.memory4Calc.InputValues[task]);
            thua = (TnThua)obj[0];
            duong = (TnDuong)obj[1];
            switch (task)
            {
                case EnumTask.landPrice3020:

                    this.startCaculator += () => calculating(ref thua, ref duong, backgroundWorker);
                    break;
                //case EnumTask.addInfo3010:
                //    this.startCaculator += () => addInfo(ref thua, ref duong, this.backgroundWorker);
                //    break;
                //case EnumTask.locthuaExtra:
                //    this.startCaculator += () => locThuaExtra(ref inputThua, "sothua = '1'");
                //    break;
                default: break;
            }
        }
        public override void RemoveTask(EnumTask task)
        {
            switch (task)
            {
                case EnumTask.landPrice3020:
                    this.startCaculator -= () => calculating(ref inputThua, ref inputDuong, backgroundWorker);
                    break;
                //case EnumTask.addInfo3010:
                //    this.startCaculator -= () => addInfo(ref inputThua, ref inputDuong, this.backgroundWorker);
                //    break;
                default: break;
            }
        }

        //public override void SetInputValues(ref TnThua thuaFc, ref TnDuong duongFc)
        //{
        //    inputThua = thuaFc;
        //    inputDuong = duongFc;
        //    //this.startCaculator = new startCaculatorDelegate( () => testBW(this.backgroundWorker));
        //    //this.startCaculator+= ()=> Calculating(ref inputThua, ref inputDuong, this.backgroundWorker);
        //}

        //public override void SetInputValues(ref TnThua thuaFc, ref TnDuong duongFc, EnumTask taskKey)
        //{
        //    object[] values = new object[] { thuaFc, duongFc };
        //    this.memory4Calc.InputValues.Add(taskKey, values);
        //}

        //public override void ClearInputValues()
        //{
        //    this.memory4Calc.InputValues.Clear();
        //}



        private void calculating(ref TnThua thuaFc, ref TnDuong duongFc, BackgroundWorker bw)
        {
            //tao layer duong
            TnExToolsFactory exToolFactory = new TnExToolsFactory();
            ITnExToolsDataManager toolDataManager = (ITnExToolsDataManager)this.dicCurrentExTools[EnumExTools.dataManager];
            ITnExToolsSpatial toolSapatial=(ITnExToolsSpatial)this.dicCurrentExTools[EnumExTools.spatialTool];
            TnGeoObject geoObject=(TnGeoObject)thuaFc;
            List<object[]> param = new List<object[]>();
            IArea a;
            //foreach(TnExtensionTools tool in this.listCurrentExTools)
            //{
            //    if(tool is ITnExToolsDataManager)
            //    {
            //        toolDataManager=(ITnExToolsDataManager)tool;
            //    }
            //    else if(tool is ITnExToolsSpatial)
            //    {
            //        toolSapatial=(ITnExToolsSpatial)tool;
            //    }
            //}
            int k=0;
            int progress = 0;
            while (duongFc.Feature != null)
            {
                k++;
                //MessageBox.Show(k.ToString());
                string dieukien = TnFieldName.DUONG_MA_DUONG+"='" + this.load.GetVal(duongFc.Feature, TnFieldName.DUONG_MA_DUONG).ToString()+"'";
                //chi clip nhung thua co maduong = duong dang xet
                this.memory4ExTool.PathLayer = toolDataManager.TnCreateFeatureLayer(duongFc.FeatureClass.AliasName, "duongLayer50", this.environment, dieukien);
                toolSapatial.TnClip(thuaFc.FeatureClass.AliasName, this.memory4ExTool.PathLayer, "tn_thua_clip2", this.environment);
                this.memory4Thua.FeatureClass = toolDataManager.TnOpenFeatureClassFromFileMdb(toolDataManager.Mem4DataManager.TempFullPathNoEnd, "tn_thua_clip2");

                //chon thua co kdat=30
                this.memory4Thua.QueryFilter.WhereClause = TnFieldName.THUA_K_DAT+"='30'";
                this.memory4Thua.FeatureCursor = this.memory4Thua.FeatureClass.Search(this.memory4Thua.QueryFilter, false);
                this.memory4Thua.Feature = this.memory4Thua.FeatureCursor.NextFeature();
                
                while (this.memory4Thua.Feature != null)
                {
                    
                    this.memory4Thua.Area = (IArea)this.memory4Thua.Feature.Shape;
                    thuaFc.QueryFilter.WhereClause = TnFieldName.THUA_SO_THUA + "='" + this.load.GetVal(this.memory4Thua.Feature, TnFieldName.THUA_SO_THUA).ToString() + "'" + " and "+TnFieldName.THUA_SO_TO+"='" + this.load.GetVal(this.memory4Thua.Feature, TnFieldName.THUA_SO_TO).ToString() + "' and "+TnFieldName.THUA_K_DAT+"='30'";
                    
                    thuaFc.FeatureCursor = thuaFc.FeatureClass.Search(thuaFc.QueryFilter, false);
                    //MessageBox.Show(thuaFc.QueryFilter.WhereClause);
                    thuaFc.Feature = thuaFc.FeatureCursor.NextFeature();
                    while (thuaFc.Feature != null)
                    {
                        //MessageBox.Show("a");
                        param.Clear();
                       
                        this.memory4Thua.Dongia = double.Parse(this.load.GetVal(thuaFc.Feature, TnFieldName.THUA_DON_GIA).ToString());
                        this.memory4Thua.Dientich1 = this.memory4Thua.Area.Area;
                        
                        this.memory4Thua.Giadat1 = this.memory4Thua.Dongia * this.memory4Thua.Dientich1;
                        this.memory4Thua.DientichTong = double.Parse(this.load.GetVal(thuaFc.Feature, TnFieldName.THUA_DIEN_TICH_TONG).ToString());
                        this.memory4Thua.Dientich2 = this.memory4Thua.DientichTong - this.memory4Thua.Dientich1;
                        this.memory4Thua.Giadat2 = this.memory4Thua.Dientich2 * this.memory4Thua.Dongia;
                        this.memory4Thua.GiadatTong = this.memory4Thua.Giadat1 + this.memory4Thua.Giadat2;
                        //MessageBox.Show(thuaFc.QueryFilter.WhereClause);
                        //MessageBox.Show(this.memory4Thua.DientichTong.ToString()+"+"+this.memory4Thua.Dientich1.ToString() + "+" + this.memory4Thua.Dientich2.ToString() + "+" + this.memory4Thua.Giadat1.ToString() + "+" + this.memory4Thua.Giadat2.ToString());
                        param.Add(new object[] { TnFieldName.THUA_DIEN_TICH_MAT_TIEN, this.memory4Thua.Dientich1 });
                        param.Add(new object[] { TnFieldName.THUA_GIA_DAT_MAT_TIEN, this.memory4Thua.Giadat1 });
                        param.Add(new object[] { TnFieldName.THUA_DIEN_TICH_SAU_50M, this.memory4Thua.Dientich2 });
                        param.Add(new object[] { TnFieldName.THUA_GIA_DAT_SAU_50M, this.memory4Thua.Giadat2 });
                        param.Add(new object[] { TnFieldName.THUA_GIA_DAT, this.memory4Thua.GiadatTong });
                        param.Add(new object[] { TnFieldName.THUA_K_DAT, "3020" });

                        this.save.SetVal(ref geoObject, param,TnConst.FEATURE);
                        thuaFc.Feature = thuaFc.FeatureCursor.NextFeature();
                    }
                    
                    //if (progress < 100)
                    //{
                    //    bw.ReportProgress(progress);
                    //}
                    //else
                    //{
                    //    bw.ReportProgress(99);
                    //}
                    //progress += 1;
                    this.memory4Thua.Feature = this.memory4Thua.FeatureCursor.NextFeature();
                }
                if (progress < 100)
                {
                    bw.ReportProgress(progress);
                }
                else
                {
                    bw.ReportProgress(99);
                }
                progress += 1;
                duongFc.Feature = duongFc.FeatureCursor.NextFeature();
            }
            this.workspaceEdit.StopEditing(true);
            bw.ReportProgress(100);
            //MessageBox.Show("3020-" + this.name+"--"+k);
        }


    }
}
